﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;

namespace SdmxMl.UC
{
    /// <summary>
    /// User control for view / edit of codelist reference
    /// </summary>
    public partial class UcCodelistRef : UserControl
    {
        #region Internal Members

        private CodeListType cl = null;
        private ArtefactList alCodelist = null;
        private ISdmxArtefactRef arRef = null;
        private bool onBinding;

        #endregion //Internal Members

        #region EventsHandler

        public event EventHandler CodelistSelect;

        public void RaiseCodelistSelect()
        {
            EventHandler eventHandler = this.CodelistSelect;
            if (eventHandler != null)
                this.CodelistSelect(this, EventArgs.Empty);
        }
        #endregion //EventsHandler

        #region Constructor

        public UcCodelistRef()
        {
            InitializeComponent();
        }
        #endregion //Constructor

        #region Properties

        public bool IsFinal { get; set; }

        public string Title
        {
            get { return groupBoxCodelistRef.Text; }
            set { groupBoxCodelistRef.Text = value; }
        }

        public string CodelistVName
        {
            get { return textBoxCodelistName.Text; }
        }

        #endregion //Properties

        #region Binding

        public void BindRef(ISdmxArtefactRef arRefer)
        {
            onBinding = true;
            arRef = arRefer;
            multiColumnComboBoxCodelist.Enabled = IsFinal == false;
            buttonAddUrl.Enabled = IsFinal == false;

            if (arRef == null)
            {
                textBoxCodelistName.Text = string.Empty;
                textBoxAgency.Text = string.Empty;
                multiColumnComboBoxCodelist.Text = null;
            }
            else
            {
                bool onError = false;
                alCodelist = InitSchemeRefControls(arRef, multiColumnComboBoxCodelist, textBoxAgency, textBoxCodelistName, out onError);
                cl = ReferenceManager.LocateCodelist(arRef);
                buttonCodelistRefError.Visible = onError;
            }

            onBinding = false;
        }

        // Helper For filling Reference controls
        public static ArtefactList InitSchemeRefControls(ISdmxArtefactRef arSchemeRef,
            MultiColumnComboBox multiCombo, TextBox tbAgency, TextBox tbNameVers, out bool onError)
        {
            ArtefactList alcrlt = ReferenceManager.GetArtefactList(SdmxArtefactType.CodeLists);
            ArtefactList alConceptSchemeRefList = new ArtefactList();
            // Sort 
            foreach (Artefact a in alcrlt)
                alConceptSchemeRefList.Add(a);

            alConceptSchemeRefList.Sort(delegate(Artefact p1, Artefact p2)
            { return p1.FullIdent.CompareTo(p2.FullIdent); });

            CodeListType cl = ReferenceManager.LocateCodelist(arSchemeRef);

            onError = false;
            string codelistName = cl == null ? string.Empty : cl.Name.ToString();
            if (arSchemeRef.Id != string.Empty && cl == null)
                onError = true;

            multiCombo.DataSource = alConceptSchemeRefList.GetTableRepresentation();
            multiCombo.ColumnWidths = multiCombo.Width.ToString() + ";" + (tbNameVers.Width + 20).ToString() + ":" + tbAgency.Width.ToString();
            if (cl != null)
                multiCombo.Text = cl.Id;
            else
                multiCombo.Text = arSchemeRef.Id;

            if (string.IsNullOrEmpty(arSchemeRef.Id))
            {
                tbNameVers.Text = string.Empty;
                tbAgency.Text = string.Empty;
                multiCombo.Text = null;

            }
            else
            {
                tbNameVers.Text = arSchemeRef.Version + " - " + codelistName;
                tbAgency.Text = arSchemeRef.AgencyId;
            }

            return alConceptSchemeRefList;
        }
        #endregion

        #region Combo Events

        private void multiColumnComboBoxCodelist_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (onBinding)
                return;
            
            DataTable dt = multiColumnComboBoxCodelist.DataSource as DataTable;
            if (dt != null )
            {
                if (multiColumnComboBoxCodelist.SelectedIndex >= 0)
                {
                    cl = alCodelist[multiColumnComboBoxCodelist.SelectedIndex] as CodeListType;
                    if (arRef != null)
                    {
                        arRef.Id = cl.Id;
                        arRef.Version = cl.Version;
                        arRef.AgencyId = cl.AgencyId;
                        textBoxAgency.Text = dt.Rows[multiColumnComboBoxCodelist.SelectedIndex][2].ToString();
                        RaiseCodelistSelect();
                    }
                }
                else if (arRef != null)
                {
                    arRef.Id = string.Empty;
                    arRef.Version = string.Empty; 
                    arRef.AgencyId = string.Empty;
                    textBoxAgency.Text = string.Empty;
                    RaiseCodelistSelect();
                }
            }
            else
            {
                textBoxCodelistName.Text = string.Empty;
                textBoxAgency.Text = string.Empty;
            }
            buttonCodelistRefError.Visible = cl == null;
        }
        #endregion

        #region Navigation

        private void buttonNavigateCodelist_Click(object sender, EventArgs e)
        {
            if (arRef != null)
            {
                try
                {
                    ArtefactRefTyped art = new ArtefactRefTyped(SdmxArtefactType.CodeLists, arRef.FullIdent);

                    // If already in memory, navigate otherwhile get from registry
                    if (NavigationManager.LocatenavigateProject(art.Type, art.FullIdent, null) == null)
                        NavigationManager.NavigateWithRegistry(art.Type, art.FullIdent, true, null);
                }
                catch { }
            }

        }
        #endregion

        #region Urn Paste

        private void buttonAddUrl_Click(object sender, EventArgs e)
        {
            try
            {
                ArtefactRefTyped art = ReferenceManager.ArTypedCopyInstance as ArtefactRefTyped;
                if (art != null)
                {
                    UrnParser p = new UrnParser(ReferenceManager.ArTypedCopyInstanceUrn);
                    if (arRef != null && p.ArType == SdmxArtefactType.CodeLists)
                    {
                        arRef.Id =p.ArtefactId;
                        arRef.Version = p.Version;
                        arRef.AgencyId = p.AgencyId;
                    //    textBoxAgency.Text = dt.Rows[multiColumnComboBoxCodelist.SelectedIndex][2].ToString();
                        BindRef(arRef);
                        RaiseCodelistSelect();
                    }
                }
            }
            catch { }
        }
        #endregion

    }
}
